import collections
from random import choice

# 一摞有序的python纸牌, namedtuple构建的对象只有属性，没有方法
Card = collections.namedtuple('Card', ['rank', 'suit'])


class FrenchDeck:
    # 2345678910JQKA
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    # 黑桃 方块 梅花 红心
    suits = 'spades diamonds clubs hearts'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for rank in self.ranks for suit in self.suits]

    # 获取长度方法
    def __len__(self):
        return len(self._cards)

    # 可迭代方法
    def __getitem__(self, item):
        return self._cards[item]


def spades_high(card):
    """
    排序函数
    :param card: 卡片
    :return: 卡序
    """
    suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * 4 + suit_values[card.suit]


deck = FrenchDeck()
print('一摞纸牌的数量：%d 张' % len(deck))
print('第一张纸牌：{}'.format(deck[0]))
print('随机抽取一张纸牌：{}'.format(choice(deck)))

# 对卡牌排序
for c in sorted(deck, key=spades_high):
    print(c)

